define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'store/preproduct/index',
                    add_url: 'store/preproduct/add',
                    edit_url: 'store/preproduct/edit',
                    del_url: 'store/preproduct/del',
                    import_url: 'store/preproduct/import',
                     multi_url: 'store/preproduct/multi',
                    table: 'preproduct',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormVisible: true,
                searchFormTemplate: 'customformtpl',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'cname', title: __('Cname')},
                        {field: 'className', title: __('Classif')},
                        {field: 'storenum', title:"库存"},
                        {field: 'uname', title: __('Uname')},
                        {field: 'ctime', title: __('Ctime'), operate:false},
                        {
                            field: 'store',
                            title: "查看库存",
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [],
                            formatter: Controller.api.formatter.view
                        },
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate
                        }
                    ]
                ],
                queryParams:function (params) {
                    var cname = $("#cname").val();
                    var first = $(".first").val();
                    var second = $(".second").val();
                    var three = $(".three").val();
                    params.filter = {};

                    if(cname!='') params.filter.cname = cname;
                    console.log();
                    if(first > 0) params.filter.first = first;
                    if(second > 0) params.filter.second = second;
                    if(three > 0) params.filter.three = three;

                    return {filter:JSON.stringify(params.filter),op:JSON.stringify(params.op),offset:JSON.stringify(params.offset),limit:JSON.stringify(params.limit)};
                }

            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        select: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'store/preproduct/select',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'cname', title: __('Cname')},
                        {field: 'className', title: __('Classif')},
                        {field: 'storenum', title: __('Storenum')},
                        {field: 'storecall', title: __('Storecall')},
                        {field: 'uname', title: __('Uname')},
                        {field: 'ctime', title: __('Ctime'), operate:false},
                        {field: 'remark', title: __('Remark')},
                        {field: 'operate', title: __('Operate'), events: {
                                'click .btn-chooseone': function (e, value, row, index) {
                                    Fast.api.close({ids: row.id, goods_name: row.cname,storenum:row.storenum, multiple: false});
                                },
                            }, formatter: function () {
                                return '<a href="javascript:;" class="btn btn-danger btn-chooseone btn-xs"><i class="fa fa-check"></i> ' + __('Choose') + '</a>';
                            }}
                    ]
                ],
                queryParams: function (params) {
                    var goods_ids = Backend.api.query('goods_ids');
                    if(goods_ids){
                        params.filter = JSON.stringify({id: goods_ids, status: 1});
                        params.op = JSON.stringify({id: 'not in', status: '='});
                        return params;
                    }

                },
            });

            // 选中多个
            $(document).on("click", ".btn-choose-multi", function () {
                var idsArr = new Array();
                $.each(table.bootstrapTable("getAllSelections"), function (i, j) {
                    idsArr.push(j);
                });
                var multiple = Backend.api.query('multiple');
                multiple = multiple == 'true' ? true : false;
                Fast.api.close({list: idsArr, multiple: multiple});
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter:{
                browser: function (value, row, index) {
                    //这里我们直接使用row的数据
                    return '<a class="btn btn-xs btn-browser" href="http://www.baidu.com">1111</a>';
                },
                view: function (value, row, index) {
                    var table = this.table;
                    // 默认按钮组
                    var buttons = [];
                    buttons.push({
                        name: 'detail',
                        title: __('查看库存'),
                        text: __('查看库存'),
                        classname: 'btn btn-primary btn-success btn-detail btn-dialog',
                        icon: 'fa fa-camera',
                        url: 'store/preproduct/view',
                        callback: function (data) {
                            Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                        }
                    });
                    return Table.api.buttonlink(this, buttons, value, row, index, 'buttons');
                },
            }
        }
    };
    return Controller;
});